﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBA.Common;

namespace DBA.Data
{
    public class SqlParasContains
    {
        public SqlParasContains(string _whereSql, string _orderSql, Dictionary<string, object> _paras)
        {
            this.whereStr = _whereSql;
            this.OrderStr = _orderSql;
            this.paras = _paras;
        }
        public string whereStr
        {
            get;
            set;
        }

        public string OrderStr
        {
            get;
            set;
        }

        public Dictionary<string, object> paras
        {
            get;
            set;
        }
    }
    public class SqlBuilder
    {
        public SqlBuilder() { }
        public SqlBuilder(IList<AdoCondition> adoCondition)
        {
            _adoCondList = adoCondition;
        }

        IList<AdoCondition> _adoCondList;

        public SqlParasContains GetSqlAndParas()
        {
            StringBuilder whereSql = new StringBuilder();
            StringBuilder OrderSql = new StringBuilder();
            IList<AdoCondition> tmpConList = new List<AdoCondition>();
            Dictionary<string, object> paras = new Dictionary<string, object>();
            
            foreach (AdoCondition item in _adoCondList)
            {
                if (item.IsSqlEnd)
                {
      
                    tmpConList.Add(item);
                }
                else
                {
                    whereSql.Append(" ").Append(item.LogicOP).Append(" ").Append(item.toSqlString());
                    CollectionsHelper.Join(paras, item.DbParas);
                }
            }
            if (tmpConList.Count > 0)
            {
                foreach (AdoCondition item in tmpConList)
                {
                    OrderSql.Append(item.toSqlString()).Append(",");
                }
            }
            return new SqlParasContains(whereSql.ToString(), OrderSql.ToString().TrimEnd(','), paras);
        }




    }
}
